package com.fuze.energy.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fuze.energy.common.Advice;
import com.fuze.energy.common.CustomException;

import java.util.Calendar;
import java.util.Date;

/**
 * @author sepc_
 * @date 8/5/2021 13:47
 * 时间格式化
 */
public class TimeUtils {

    /**
     * 截取时间的小时数和分钟数并拼接成字符串
     *
     * @param date 时间
     * @return 小时数和分钟数拼接成的字符串
     */
    public static String getTimeStr(Date date) {

        if (date == null) {
            throw new CustomException(Advice.FAILED_QUERY);
        }
        int limit = 10;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        StringBuilder sb = new StringBuilder();
        if (hour < limit) {
            sb.append(0).append(hour);
        } else {
            sb.append(hour);
        }
        sb.append(":");
        if (minute < limit) {
            sb.append(0).append(minute);
        } else {
            sb.append(minute);
        }
        return sb.toString();
    }

    /**
     * 获取当前一天的开始时间
     */
    public static DateTime getBegin() {
        DateTime date = DateUtil.date();
        return DateUtil.beginOfDay(date);
    }

    /**
     * 获取当前一天的结束时间
     */
    public static DateTime getEng() {
        DateTime date = DateUtil.date();
        return DateUtil.endOfDay(date);
    }

}
